﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Web.Mvc;

namespace TestHttpModules.Models
{
    public class ShoppingCart : List<ShoppingCartItem>
    { }
    #region 自定义验证
    public class CustomerMetaData
    {
        [ScaffoldColumn(false)]
        public object Id { get; set; }
        // [ScaffoldColumn(false)] //是否在页面上显示，一般主键设为false,默认为true
        [Required()]
        [RegularExpression(@"^[a-zA-Z''-'\s]{6,40}$", ErrorMessage = "Entered characters are not allowed.")]
        public Object Name { get; set; }
        [Required()]
        [RegularExpression(@"^[0-9''-'\s]{1,10}$", ErrorMessage = "Entered characters must is number.")]
        public Object Quantity { get; set; }
        [DisplayName("真实姓名")]
        [Required(ErrorMessage = "请输入{0}!")]
        [StringLength(8, ErrorMessage = "作为{0}字符串长度不能超过{1}!")]
        public string RealName { get; set; }
        [DisplayName("电子邮箱地址")]
        [RegularExpression(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$", ErrorMessage = "请输入正确的电子邮箱地址！")]
        public string Email { get; set; }
        [AgeRange(18, 30, ErrorMessage = "{0}必须在{1}和{2}之间！")]
        [DisplayName("出生日期")]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
        public DateTime Birthday { get; set; }
    }

    [MetadataType(typeof(CustomerMetaData))]
    public class ShoppingCartItem
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string RealName { get; set; }
        public int Quantity { get; set; }
        public string Email { get; set; }

        public DateTime Birthday { get; set; }
    }
    #endregion


    /// <summary>
    /// 以日期作为出生日期验证特性，指定出生日期在某个年龄范围内
    /// </summary>
    public class AgeRangeAttribute : RangeAttribute, IClientValidatable
    {
        public AgeRangeAttribute(int minimum, int maximum)
            : base(minimum, maximum)
        { }
        public override bool IsValid(object value)
        {
            DateTime birthDate = (DateTime)value;
            DateTime age = new DateTime(DateTime.Now.Ticks - birthDate.Ticks);
            return age.Year >= (int)this.Minimum && age.Year <= (int)this.Maximum;
        }
        public override string FormatErrorMessage(string name)
        {
            return base.FormatErrorMessage("年龄");
        }
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            ModelClientValidationRule validationRule = new ModelClientValidationRule()
            {
                ValidationType = "agerange",
                ErrorMessage = FormatErrorMessage(metadata.DisplayName)
            };
            validationRule.ValidationParameters.Add("currentdate", DateTime.Today.ToString("dd-MM-yyyy"));
            validationRule.ValidationParameters.Add("minage", this.Minimum);
            validationRule.ValidationParameters.Add("maxage", this.Maximum);
            yield return validationRule;

        }

    }



}